package com.winit.openapi.wh.outbound.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.outbound.OutboundWinitProductService;
import com.winit.oms.spi.outbound.command.order.OutboundOrderBillingCommand;
import com.winit.oms.spi.outbound.command.order.QueryUsableWinitProductCommand;
import com.winit.oms.spi.outbound.vo.OutboundOrderBillingVo;
import com.winit.oms.spi.v2.WinitProductRecommendService;
import com.winit.oms.spi.v2.tools.command.ProductRecommendCommand;
import com.winit.oms.spi.v2.tools.request.ProductRecommendRequestParams;
import com.winit.oms.spi.v2.tools.result.ProductRecommendProductResult;
import com.winit.oms.spi.vo.v2.response.QueryUsableWinitProductResult;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.outbound.builder.OutboundOrderBuilder;
import com.winit.openapi.wh.outbound.builder.OutboundOrderWinitServiceBuilder;
import com.winit.openapi.wh.outbound.validator.OutboundOrderfieldValidator;

/**
 * <winit 产品查询>
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * Administrator 	1.0  		2016-5-17 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/outbound")
public class OutboundOrderWinitServiceController extends BaseController {

    @Resource
    private OutboundWinitProductService outboundWinitProductService;
    @Resource
    private WinitProductRecommendService winitProductRecommendService;

    /**
     * 产品查询
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/winitProductByOrderNo")
    public String getOrderWinitServiceByOrderNo(@ModelAttribute RequestMsg requestMsg,
                                                @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryUsableWinitProductCommand command = new QueryUsableWinitProductCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            command.setOrderNo(json.getString("orderNo"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data"); 
        }
        // 调用接口进行产品查询
        List<QueryUsableWinitProductResult> result = outboundWinitProductService.getUsableWinitProduct(command); 
        responseMsg.setData(OutboundOrderWinitServiceBuilder.buildWinitProductList(result));
        responseMsg.setMsg("SUCCESS");
        return SUCCESS;
    }

    
    /**
     * 计价工具(请使用newValuationTool接口代替当前接口)
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/valuationTool")
    @Deprecated
    public String valuationTool(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        OutboundOrderBillingCommand command = new OutboundOrderBillingCommand();
        List<QueryUsableWinitProductResult> list = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            OutboundOrderfieldValidator.validParam(json);
            OutboundOrderBillingVo vo = OutboundOrderBuilder.buildValuationTool(json);
            if (vo != null) {
                command.setBillingVo(vo);
                list = outboundWinitProductService.getValuationTool(command);
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 调用接口进行产品组装
        responseMsg.setData(OutboundOrderWinitServiceBuilder.buildWinitProductList(list));
        responseMsg.setMsg("SUCCESS");
        return SUCCESS;
    }

    /**
     * 计价工具
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/newValuationTool")
    public String newValuationTool(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        ProductRecommendRequestParams requestParams = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            OutboundOrderfieldValidator.newValidParam(json);
            requestParams  = OutboundOrderBuilder.buildNewValuationTool(json);
            requestParams.setIsValidateEgd(false);
        }
        // 组装command
        ProductRecommendCommand command = new ProductRecommendCommand(); 
        command.setCtx(CommandContext.getContext());
        command.setRequestParams(requestParams);
        ProductRecommendProductResult result = winitProductRecommendService.recommend(command);
        responseMsg.setData(OutboundOrderWinitServiceBuilder.buildCalcDeliveryFeeFeeList(result,null));
        responseMsg.setMsg("SUCCESS");
        return SUCCESS;
    }
    
     @RequestMapping(value = "/calcDeliveryFee")
     public String calcDeliveryFee(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
         Object obj = requestMsg.getData();
         ProductRecommendRequestParams requestParams = null;
         if (obj instanceof JSONObject) {
             JSONObject json = (JSONObject) obj;
             OutboundOrderfieldValidator.validCalcDeliveryParam(json);
             requestParams  = OutboundOrderBuilder.buildValuationCalcDeliveryFeeTool(json);
             requestParams.setIsValidateEgd(false);
         }
         // 组装command
         ProductRecommendCommand command = new ProductRecommendCommand(); 
         command.setCtx(CommandContext.getContext());
         command.setRequestParams(requestParams);
         ProductRecommendProductResult result = winitProductRecommendService.recommend(command);
         responseMsg.setData(OutboundOrderWinitServiceBuilder.buildCalcDeliveryFeeFeeList(result,command.getRequestParams().getWinitProductCode()));
         responseMsg.setMsg("SUCCESS");
         return SUCCESS;
     }
     
     /**
      * 尾程派送费用计算器
      * 由于calcDeliveryFee接口里面有坑，并且对外开放，不影响这个接口，特开个接口供卖家网站使用
      * @param requestMsg
      * @param responseMsg
      * @return
      */
     @RequestMapping(value = "/deliveryFeeCalculator")
     public String deliveryFeeCalculator(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
         Object obj = requestMsg.getData();
         ProductRecommendRequestParams requestParams = null;
         if (obj instanceof JSONObject) {
             JSONObject json = (JSONObject) obj;
             OutboundOrderfieldValidator.validCalWHDeliveryParam(json);
             requestParams  = OutboundOrderBuilder.buildCalWHDeliveryParam(json);
             requestParams.setIsValidateEgd(false);
         }
         // 组装command
         ProductRecommendCommand command = new ProductRecommendCommand(); 
         command.setCtx(CommandContext.getContext());
         command.setRequestParams(requestParams);
         ProductRecommendProductResult result = winitProductRecommendService.recommend(command);
         responseMsg.setData(OutboundOrderWinitServiceBuilder.buildCalWHDeliveryFeeFeeList(result,command.getRequestParams().getWinitProductCode()));
         responseMsg.setMsg("SUCCESS");
         return SUCCESS;
     }
}
